<?php
if (!defined('BASEPATH'))exit('No direct script access allowed');
require_once('fpdf/fpdf.php');
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of cetakbayar_pdf
 *
 * @author miyzan
 */
class CetakVoucher_pdf extends FPDF {
    //put your code here
//    'courier'
    var $cetak;
    var $lineh;
    var $revdata;
    function Header() {

//Logo
//        $this->Image('assets/img/logo-mbs.jpg', 5, 5, 25);
//Arial bold 15

        $this->SetFont('courier', 'B', 12);
//Move to the right
        $this->Cell(70,5,'PT.SURYA KENCANA KERAMINDO');
        
        $dtrevisi=$this->revdata;
        if(count($dtrevisi)>0){
           if($dtrevisi['revisi_date']){
           $dtrevisi['revisi_date']=date('d-M-Y',strtotime($dtrevisi['revisi_date']));
       }
       }
       $this->SetFont('courier', 'I', 8);
       $revisi='';
       if($dtrevisi['revisike'] && $dtrevisi['revisike']>0){
        $revisi='RevisiKe:'.$dtrevisi['revisike'].' TanggalRevisi:'.$dtrevisi['revisi_date'].' RevisiBy:'.$dtrevisi['revisi_by'];
       }
        $this->Cell(126,5,$revisi,0,0,'R');
//        $this->SetDrawColor(128,0,0);
//        $this->SetLineWidth(.5);
//        $this->Line(5, 20, 210, 20);
//Title
//        $this->Cell(10, 10, 'Neraca', 0, 0, 'C');
//Line break
        $this->Ln(6);
    }    
    
    
   
    function Footer() {
        //Position at 1.5 cm from bottom
        $this->SetY(-25);
        $this->SetFont('courier', 'I', 8);
        //Page number
        $dtcetak=$this->getCetak();
       if(count($dtcetak)>0){
//           echo var_dump($dtcetak);
           if($dtcetak['tglcetak']){
           $dtcetak['tglcetak']=date('d-M-Y',strtotime($dtcetak['tglcetak'])).' '.date('H:i:s');
       }
       }
        $pg='Page '. $this->PageNo().' of {nb}';
         $this->Cell(140, 5, 'CetakanKe:'.$dtcetak['cetakke'].' TanggalCetak:'.$dtcetak['tglcetak'] .' CetakBy:'.$dtcetak['cetakby'], 0, 0, 'L');         
         $this->Cell(56, 5, $pg , 0, 0, 'R');        
    }
    
    function set_subtitle($h) {
//        $this->$cetak='far';
        if(count($h)>0){
            $filter=$h[0]->title;
            $this->SetFont('courier', 'B', 12);
            $l=(205/2)-(strlen($filter)/2);
            $this->Cell($l);
            $this->Cell(9, 5, $filter, 0, 0, 'C');
            $this->Ln();

            $w = array(20,4, 30, 18,4,42, 25, 4, 50);        

            $this->SetFont('courier', '', 8);        

            $this->Cell($w[0], 5, 'Tgl.Voucher', 0, 0, 'L', false);
            $this->Cell($w[1], 5, ':', 0, 0, 'C', false);
            
            if($h[0]->tgl_transaksi){
                $tgltrx=date('d F Y',strtotime($h[0]->tgl_transaksi));
            }
            
            $this->Cell($w[2], 5, $tgltrx, 0, 0, 'L', false);

            $this->Cell($w[3], 5, 'Tgl.Jurnal', 0, 0, 'L', false);
            $this->Cell($w[4], 5, ':', 0, 0, 'C', false);
            $tgl='';
            if($h[0]->posting_date){
                $tgl=date('d F Y',strtotime($h[0]->posting_date));
            }

            $this->Cell($w[5], 5, $tgl, 0, 0, 'L', false);

            $this->Cell($w[6], 5, 'No.Giro/Cheque', 0, 0, 'L', false);
            $this->Cell($w[7], 5, ':', 0, 0, 'C', false);
            $this->Cell($w[8], 5, $h[0]->no_giro_cheque, 0, 0, 'L', false);


            $this->Ln(3);

            $this->Cell($w[0], 5, 'No.Voucher', 0, 0, 'L', false);
            $this->Cell($w[1], 5, ':', 0, 0, 'C', false);
            $this->Cell($w[2], 5, $h[0]->kd_voucher, 0, 0, 'L', false);
            $this->Cell($w[3], 5, 'No.Jurnal', 0, 0, 'L', false);
            $this->Cell($w[4], 5, ':', 0, 0, 'C', false);
            $this->Cell($w[5], 5, $h[0]->idjurnal, 0, 0, 'L', false);
            $this->Cell($w[6], 5, 'Tgl.Jatuhtempo', 0, 0, 'L', false);
            $this->Cell($w[7], 5, ':', 0, 0, 'C', false);
            if($h[0]->tgl_jttempo){
                $tgljttempo=date('d F Y',strtotime($h[0]->tgl_jttempo));
            }
            $this->Cell($w[8], 5, $tgljttempo, 0, 0, 'L', false);

            $this->Ln(3);

            $this->Cell($w[0], 5, 'Keterangan', 0, 0, 'L', false);
            $this->Cell($w[1], 5, ':', 0, 0, 'C', false);
            $this->Cell(179, 5, $h[0]->keterangan, 0, 0, 'L', false);
            $this->Ln(6);
        }
        
        
        
    }    
    function set_header_column($w) {
        $header = array('Kode Akun','Nama Akun', 'Cost Center', 'Keterangan', 'Debet', 'Kredit');
        $this->SetFont('courier', '', 8);
        for($i=0;$i<count($w);$i++) $this->Cell($w[$i], 4, $header[$i], 1, 0, 'C', false);               
        $this->Ln();
    }
    function setLegal($h){
        $legal=array('Voucher Entry','Approval1','Approval2', 'Approval3', 'Diterima Oleh', 'Dikeluarkan Oleh');
        $wlegal = array(32,32, 32, 32, 32, 32); 
        
        $this->SetFont('courier', '', 8);        
        $this->Cell($wlegal[0], 3, $legal[0], 0, 0, 'C', false);
        if($h[0]->approval1 == 1){
            $this->Cell($wlegal[1], 3, $legal[1], 0, 0, 'C', false);
        }else{
            $this->Cell($wlegal[1], 3, '', 0, 0, 'C', false);
        }
        if($h[0]->approval2 == 1){
            $this->Cell($wlegal[2], 3, $legal[2], 0, 0, 'C', false);
        }else{
            $this->Cell($wlegal[2], 3, '', 0, 0, 'C', false);
        }
        if($h[0]->approval3 == 1){
            $this->Cell($wlegal[3], 3, $legal[3], 0, 0, 'C', false);
        }else{
            $this->Cell($wlegal[3], 3, '', 0, 0, 'C', false);
        }
        $this->Cell($wlegal[5], 3, $legal[5], 0, 0, 'C', false);
        if($h[0]->diterima_oleh){
            $this->Cell($wlegal[4], 3, $legal[4], 0, 0, 'C', false);
        }else{
            $this->Cell($wlegal[4], 3, '', 0, 0, 'C', false);
        }
        
        
        $this->Ln();
        $tglcd='';
        $tglapp1='';
        $tglapp2='';
        $tglapp3='';
     
            if($h[0]->created_date){
                $tglcd=date('d-M-Y',strtotime($h[0]->created_date));
            }
     
        $this->Cell($wlegal[0], 4, $tglcd, 0, 0, 'C', false);
        if($h[0]->approval1 == 1){
            if($h[0]->approval_date){
                $tglapp1=date('d-M-Y',strtotime($h[0]->approval_date));
            }
            $this->Cell($wlegal[1], 4, $tglapp1, 0, 0, 'C', false);
        }else{
            $this->Cell($wlegal[1], 4, '', 0, 0, 'C', false);
        }
        if($h[0]->approval2 == 1){
            if($h[0]->approval2_date){
                $tglapp2=date('d-M-Y',strtotime($h[0]->approval2_date));
            }
            $this->Cell($wlegal[2], 4, $tglapp2, 0, 0, 'C', false);
        }else{
            $this->Cell($wlegal[2], 4, '', 0, 0, 'C', false);
        }
        
        if($h[0]->approval3 == 1){
            if($h[0]->approval3_date){
                $tglapp3=date('d-M-Y',strtotime($h[0]->approval3_date));
            }
            $this->Cell($wlegal[3], 4, $tglapp3, 0, 0, 'C', false);
        }else{
            $this->Cell($wlegal[3], 4, '', 0, 0, 'C', false);
        }
        $this->Cell($wlegal[5], 4, '', 0, 0, 'C', false);
        $this->Cell($wlegal[4], 4, '', 0, 0, 'C', false);
        
        
        $this->Ln();
        $this->Ln();
        $this->Ln(2);        
        
        $this->Cell($wlegal[0], 4, $h[0]->created_by, 0, 0, 'C', false);
        if($h[0]->approval1 == 1){
            $this->Cell($wlegal[1], 4, $h[0]->app1jabatan, 0, 0, 'C', false);
//            $this->Cell($wlegal[1], 4, $h[0]->approval_by, 0, 0, 'C', false);
        }else{
            $this->Cell($wlegal[1], 4, '', 0, 0, 'C', false);
        }
        if($h[0]->approval2 == 1){
            $this->Cell($wlegal[2], 4, $h[0]->app2jabatan, 0, 0, 'C', false);
//            $this->Cell($wlegal[2], 4, $h[0]->approval2_by, 0, 0, 'C', false);
        }else{
            $this->Cell($wlegal[2], 4, '', 0, 0, 'C', false);
        }
        if($h[0]->approval3 == 1){
            $this->Cell($wlegal[3], 4, $h[0]->app3jabatan, 0, 0, 'C', false); 
//            $this->Cell($wlegal[3], 4, $h[0]->approval3_by, 0, 0, 'C', false); 
        }else{
            $this->Cell($wlegal[3], 4, '', 0, 0, 'C', false);
        }
               
        $this->Cell($wlegal[5], 4, '', 0, 0, 'C', false);
        if($h[0]->diterima_oleh){
            $this->Cell($wlegal[4], 4, $h[0]->diterima_oleh, 0, 0, 'C', false);
        }{
            $this->Cell($wlegal[4], 4, '', 0, 0, 'C', false);
        }
        
    }
    function setCetak($c=NULL){
        $this->cetak=$c;
    }
    
    function getCetak(){
        return $this->cetak;
    }
    function setRevisi($c=NULL){
        $this->revdata=$c;
    }    
    function getRevisi(){
        return $this->revdata;
    }
    function create_pdf($h, $d) {
        $this->SetAutoPageBreak(true,25);
        $this->set_subtitle($h);
//        $this->setCetak('aa');        
        
        
//        $wcolheader = array(16,40, 25, 65, 25, 25);  
        $this->SetWidths(array(16,40, 25, 59, 28, 28));
        $this->SetAligns(array('C','L', 'L', 'L', 'R', 'R'));  
        $this->SetFont('courier', '', 8);     
        $this->set_header_column($this->widths);
        $totald=0;
        $totalk=0;
        foreach ($d as $baris) {
			$this->Row(
				array(
				$baris->kd_akun,
				$baris->nama,
				$baris->costcenter,
				$baris->keterangan_detail,
				$baris->debet,
                                    $baris->kredit
			),$h);
                        $totald +=$baris->debet;
                        $totalk +=$baris->kredit;
                        
		}
                $this->CheckPageBreakLegal($this->lineh+1,$h);
                $this->Cell($this->widths[0], 4, '', 'LTB', 0, 'C', false);
                $this->Cell($this->widths[1], 4, '', 'TB', 0, 'L', false);
                $this->Cell($this->widths[2], 4, '', 'TB', 0, 'L', false);
                $this->Cell($this->widths[3], 4, 'TOTAL', 'TB', 0, 'R', false);
                $this->Cell($this->widths[4], 4, number_format($totald), 1, 0, 'R', false);
                $this->Cell($this->widths[5], 4, number_format($totalk), 1, 0, 'R', false); 
                
                
                $this->CheckPageBreakLegal($this->lineh+1+20,$h);
                $this->Ln(6);
                $this->setLegal($h);
//                
    }
    //===============
        private $widths;
	private $aligns;
 
	function SetWidths($w)
	{
		//Set the array of column widths
		$this->widths=$w;
	}
 
	function SetAligns($a)
	{
		//Set the array of column alignments
		$this->aligns=$a;
	}
 
	function Row($data,$st)
	{
		//Calculate the height of the row
		$nb=0;
		for($i=0;$i<count($data);$i++)
			$nb=max($nb,$this->NbLines($this->widths[$i],$data[$i]));
		$h=4*$nb;
		//Issue a page break first if needed
		$this->CheckPageBreak($h,$st);
		//Draw the cells of the row
		for($i=0;$i<count($data);$i++)
		{
			$w=$this->widths[$i];
			$a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';
			//Save the current position
			$x=$this->GetX();
			$y=$this->GetY();
			//Draw the border
			$this->Rect($x,$y,$w,$h);
			//Print the text
                        if(is_numeric($data[$i])){
                            $data[$i]=number_format($data[$i]);
                        }
			$this->MultiCell($w,4,$data[$i],0,$a);
			//Put the position to the right of the cell
			$this->SetXY($x+$w,$y);
                       
                        
                        
                        
		}
                
		//Go to the next line
		$this->Ln($h);
                $this->lineh=$h;
                
                
	}
 
        function CheckPageBreakLegal($h,$st=array())
	{
		//If the height h would cause an overflow, add a new page immediately
		if($this->GetY()+$h>$this->PageBreakTrigger){
                    	$this->AddPage($this->CurOrientation);
                        $this->set_subtitle($st);
//                        $this->set_header_column($this->widths);
                }
		
                
	}
	function CheckPageBreak($h,$st=array())
	{
		//If the height h would cause an overflow, add a new page immediately
		if($this->GetY()+$h>$this->PageBreakTrigger){
                    	$this->AddPage($this->CurOrientation);
                        $this->set_subtitle($st);
                        $this->set_header_column($this->widths);
                }
		
                
	}
 
	function NbLines($w,$txt)
	{
		//Computes the number of lines a MultiCell of width w will take
		$cw=&$this->CurrentFont['cw'];
		if($w==0)
			$w=$this->w-$this->rMargin-$this->x;
		$wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
		$s=str_replace("\r",'',$txt);
		$nb=strlen($s);
		if($nb>0 and $s[$nb-1]=="\n")
			$nb--;
		$sep=-1;
		$i=0;
		$j=0;
		$l=0;
		$nl=1;
		while($i<$nb)
		{
			$c=$s[$i];
			if($c=="\n")
			{
				$i++;
				$sep=-1;
				$j=$i;
				$l=0;
				$nl++;
				continue;
			}
			if($c==' ')
				$sep=$i;
			$l+=$cw[$c];
			if($l>$wmax)
			{
				if($sep==-1)
				{
					if($i==$j)
						$i++;
				}
				else
					$i=$sep+1;
				$sep=-1;
				$j=$i;
				$l=0;
				$nl++;
			}
			else
				$i++;
		}
		return $nl;
	}
//===================================================        
    function create_pdf2($h, $d) {
//        $this->set_subtitle($h);
        $this->setCetak('aa');
        
        $detail=array();
        
        for($i=0;$i<17;$i++){
            array_push($detail,array('330.0003','Biaya Listrik', 'Accounting', 'Test Validasi', '21.000.000.000', '0'));
        }
        
        $wcolheader = array(16,40, 25, 65, 25, 25);  
//        $this->set_header_column($wcolheader);
//        $this->Ln();
        $lg=17;
        $j = 0;
        $totald=0;
        $totalk=0;
        $part=0;
        for($i=0;$i<count($d);$i++){
            if (($j % 11) == 0) {
                if ($j != 0) {
                    $this->AddPage('P');
                }
                $this->set_subtitle($h);
//                $this->Ln(5);
                $this->set_header_column($wcolheader);
                $part=0;
            }
            $this->Cell($wcolheader[0], 4, $d[$i]->kd_akun, 1, 0, 'C', false);
            $this->Cell($wcolheader[1], 4, $d[$i]->nama, 1, 0, 'L', false);
            $this->Cell($wcolheader[2], 4, $d[$i]->costcenter, 1, 0, 'L', false);
            $this->Cell($wcolheader[3], 4, $d[$i]->keterangan_detail, 1, 0, 'L', false);
            $this->Cell($wcolheader[4], 4, number_format($d[$i]->debet), 1, 0, 'R', false);
            $this->Cell($wcolheader[5], 4, number_format($d[$i]->kredit), 1, 0, 'R', false); 
            $totald = $totald + $d[$i]->debet;
            $totalk = $totalk + $d[$i]->kredit;
            if($i==count($d)-1){
                $this->Ln();
                $this->Cell($wcolheader[0], 4, '', 'LB', 0, 'C', false);
                $this->Cell($wcolheader[1], 4, '', 'B', 0, 'L', false);
                $this->Cell($wcolheader[2], 4, '', 'B', 0, 'L', false);
                $this->Cell($wcolheader[3], 4, 'TOTAL', 'B', 0, 'R', false);
                $this->Cell($wcolheader[4], 4, number_format($totald), 1, 0, 'R', false);
                $this->Cell($wcolheader[5], 4, number_format($totalk), 1, 0, 'R', false); 
                $this->Ln(6);
            }else{
                $this->Ln();
            }     
            $j++; 
            $part++;
        }      
        if($part<6 || $part==6){
            $this->setLegal($h);
        }elseif ($part>6) {
            
            $this->AddPage('P');
            $this->set_subtitle($h);
            $this->setLegal($h);
        }
        
                      
        
        
        
        
    }
}

?>
